/*
 * @lc app=leetcode.cn id=6 lang=golang
 *
 * [6] Z 字形变换
 */

// @lc code=start
func convert(s string, numRows int) string {
	if numRows <= 1 {
		return s
	}
	var b1 strings.Builder
	l := len(s)
	lop := numRows*2 - 2
	for i := 0; i < l; i += lop {
		b1.WriteByte(s[i])
	}
	for i := 1; i < numRows-1; i++ {
		for m, n := i, lop-i; ; m, n = m+lop, n+lop {
			if m < l {
				b1.WriteByte(s[m])
			} else {
				break
			}
			if n < l {
				b1.WriteByte(s[n])
			} else {
				break
			}
		}
	}
	for i := numRows - 1; i < l; i += lop {
		b1.WriteByte(s[i])
	}
	return b1.String()
}

// @lc code=end

